package com.example.searchservice.controller;

import com.example.cloudcommon.feign.CenterLogsClient;
import com.example.cloudcommon.feign.CenterUserClient;
import com.example.searchservice.doc.UserDocument;
import com.example.searchservice.repository.SearchResult;
import com.example.searchservice.repository.UserRepository;
import com.example.searchservice.service.impl.UserServiceImpl;
import com.example.searchservice.service.impl.WordSegmentationServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/users")
@Slf4j
public class UserController {

    @Autowired
    private CenterUserClient centerUserClient;

    @Autowired
    private CenterLogsClient centerLogsClient;;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private Gson gson;

    @Autowired
    private WordSegmentationServiceImpl wordSegmentationService;

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private UserRepository userRepository;

    @GetMapping("/segment")
    public List<String> segmentText(@RequestParam String text) {
        try {
            return wordSegmentationService.segment(text);
        } catch (Exception e) {
            throw new RuntimeException("分词处理失败", e);
        }
    }

    @GetMapping("/users/hello")
    public ResponseEntity<?> hello() throws Exception {
        return ResponseEntity.ok(centerUserClient.users());

    }

    @GetMapping("/operate/logs")
    public ResponseEntity<?> logs() throws Exception {
        return ResponseEntity.ok(centerLogsClient.oplogs());
    }

    @PostMapping
    public ResponseEntity<UserDocument> createUser(@RequestBody UserDocument user) {
        UserDocument savedUser = null;
        try {
            savedUser = userService.saveUser(user);
        } catch (Exception e) {
            log.error("创建用户失败", e);
        }
        return ResponseEntity.ok(savedUser);
    }

    @GetMapping("/{id}")
    public ResponseEntity<UserDocument> getUser(@PathVariable String id) {
        return userRepository.findById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @GetMapping("/search")
    public ResponseEntity searchUsers(@RequestParam String q) {
        return ResponseEntity.ok(new SearchResult(userService.searchUsers(q)));
    }

    @GetMapping("/index-info")
    public Map<String, Object> getIndexInfo() {
        return userService.getIndexInfo();
    }

    @PostMapping("/init-index")
    public ResponseEntity<String> initIndex() {
        userService.createIndexIfNotExists();
        return ResponseEntity.ok("索引初始化完成");
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteUser(@PathVariable String id) {
        userRepository.deleteById(id);
        return ResponseEntity.noContent().build();
    }

    @Operation(summary = "中文分词查询")
    @GetMapping("/search/ik")
    public ResponseEntity chineseFindByNameDesc(@RequestParam String q) {
        log.info("q: {}", q);
        userService.diagnoseAnalyzer();
        userService.checkIndexMapping();
        return ResponseEntity.ok(userService.chineseFindByNameDesc(q, 0, 10));
//        return ResponseEntity.ok(userService.safeChineseSearch( q));
    }

    @GetMapping("/test")
    public ResponseEntity testIkAnalyzer() {
        return ResponseEntity.ok(userService.testIkAnalyzer());
    }

}